
layui.use(['layer','table','laydate'],function(){
    $ = layui.jquery;
    var layer = layui.layer ,table = layui.table,laydate = layui.laydate;
    //第一个实例
    table.render({
        elem: '#demo'
        ,id:'tabuser'
        ,height: "auto"
        ,method:"get"
        ,url: baseUrl+'/admin/userinfo' //数据接口
        ,request: {
            pageName: 'pageNum' //页码的参数名称，默认：page
            ,limitName: 'pageSize' //每页数据量的参数名，默认：limit
        }
        ,page: true //开启分页
        ,cols: [[ //表头

             {field: 'nickname', title: '昵称',align:'center',sort:true}
            ,{field: 'photo', title: '头像',align:'center', event: 'lookimg1',templet:'#showimg'}
            ,{field: 'phone', title: '电话',align:'center',sort:true}
            ,{fixed: 'right', title: '操作', width:150, align:'left', toolbar: '#barDemo'} //这里的toolbar值是模板元素的选择器
        ]]
    });
    //监听工具条
    table.on('tool(test)', function(obj){
        var data = obj.data;
        var layEvent = obj.event;
        var tr = obj.tr; //获得当前行 tr 的DOM对象
        if(layEvent === 'del'){ //删除
    	    layer.confirm('真的删除行么', function(index){
    	      obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
    	      layer.close(index);
    	      //向服务端发送删除指令
    	       $.ajax({
    			  type: 'post',
    			  url: baseUrl+"/admin/delete",
    			  data:{id:data.id},
    			  dataType: 'json',
    			  success: function(res){
    			  	if(res.meta.code == 200){
    			  		layer.msg(''+res.meta.msg+'',{icon:1,time:1500});
    			  	}else {
    			  		layer.msg(''+res.meta.msg+'',{icon:2,time:1500});
    			  		return;
    			  	}
    			  }
    			});
    	    });
    	  } else if(layEvent === 'lookimg1'){
            var arr = subString(data.headimg);
            var data = "";
            for (var i = 0 ; i < arr.length ; i++){
                data+='<img style="text-align: center" src="'+baseUrl+"/"+arr[i]+'">';
            }
            layer.open({
                title: '查看图片'
                ,area: ['100%', '100%']
                ,content: data
            });
        }else if(layEvent === 'edit') { //编辑
            var type = 0;
            if (data.type==0){
                type = 1;
            }
            //询问框
            layer.confirm('确定修改用户类型？', {
                btn: ['确定','取消'] //按钮
            }, function(){
                var uinfo = {'id': data.id , "type" : type }
                //这里一般是发送修改的Ajax请求
                $.ajax({
                    type: 'post',
                    url: baseUrl+"/manage/updateUser",
                    data: uinfo,
                    async:false,
                    dataType: 'json',
                    success: function(res){
                        if(res.code==100){
                            //加载层
                            var index = layer.load(0, {shade: false,time:500} ); //0代表加载的风格，支持0-2
                            setTimeout(function(){ layer.msg(''+res.msg+'',{icon:1,time:1000});index.closed}, 500);
                            setTimeout(function(){ location.reload() }, 1000);
                        }else{
                            //加载层
                            var index = layer.load(0, {shade: false,time:1000} ); //0代表加载的风格，支持0-2
                            setTimeout(function(){ layer.msg(''+res.msg+'',{icon:2,time:1000});index.closed}, 1000);
                        }
                    }
                });
            }, function(){
                //取消
            });
        }else if(layEvent === "editisAc"){
            if(data.isAc===1){
                var index = layui.layer.open({
                    title: "审核",
                    type: 2,
                    content: "editisAc.html",
                    success: function (layero, index) {
                        var body = layui.layer.getChildFrame('body', index);
                        body.find("#id").val(data.id);
                        setTimeout(function () {
                            layui.layer.tips('点击此处返回', '.layui-layer-setwin .layui-layer-close', {
                                tips: 3
                            });
                        }, 500)
                    }
                });
                layui.layer.full(index);
                //改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
                $(window).on("resize",function(){
                    layui.layer.full(index);
                })
            }
        }
    });
})
